# vim: ft=dockerfile
ARG BASE_IMAGE=docker.io/ecpe4s/ubuntu22.04-runner-amd64-gcc-11.4:2025.03.01
FROM $BASE_IMAGE

ARG SPACK_VERSION=v1.0.2
ARG SPACK_MIRROR="https://binaries.spack.io/v2025.07.0"
ARG ENABLED_ENVS="serial,ompi,mpich"
ARG EXTERNAL_PACKAGES
ARG EXTRA_VARIANTS
ARG PATCH_VARIANT_XROOTD


# Clone and patch spack
WORKDIR /
RUN if ! [ -d /spack ]; then \
      git clone --depth 1 --single-branch --branch ${SPACK_VERSION} https://github.com/spack/spack; \
    else \
      git fetch --all && git checkout -t origin/${SPACK_VERSION}; \
    fi

COPY 0002-adios2-add-xrootd-variant.patch /spack

COPY packages.yaml /etc/spack/packages.yaml

WORKDIR /spack

# Install base specs
RUN . /spack/share/spack/setup-env.sh && \
    if test -n "${EXTERNAL_PACKAGES}"; then spack external find ${EXTERNAL_PACKAGES}; fi && \
    spack info adios2 && \
    if test -n "${PATCH_VARIANT_XROOTD}"; then cd ~/.spack/package_repos/fncqgg4 && git apply /spack/0002-adios2-add-xrootd-variant.patch && cd /spack; fi && \
    spack mirror add binaries "${SPACK_MIRROR}" && \
    export ADIOS_SPEC_NO_MPI="adios2~mpi${EXTRA_VARIANTS}" && \
    export ADIOS_SPEC_OPENMPI="adios2+mpi${EXTRA_VARIANTS} ^openmpi" && \
    export ADIOS_SPEC_MPICH="adios2+mpi${EXTRA_VARIANTS} ^mpich" && \
    spack config add "config:checksum:false" && \
    spack config add "config:build_jobs:$(nproc)" && \
    spack config add "concretizer:unify:true" && \
    if echo "$ENABLED_ENVS" | grep -q serial; then \
    spack env create adios2-ci-serial && \
    spack -e adios2-ci-serial add ${ADIOS_SPEC_NO_MPI} && \
    spack -e adios2-ci-serial concretize && \
    spack -e adios2-ci-serial install \
      --include-build-deps \
      --no-check-signature \
      --fail-fast \
      --only dependencies; \
    fi && \
    if echo "$ENABLED_ENVS" | grep -q ompi ; then \
    spack env create adios2-ci-ompi && \
    spack -e adios2-ci-ompi add ${ADIOS_SPEC_OPENMPI} && \
    spack -e adios2-ci-ompi concretize && \
    spack -e adios2-ci-ompi install \
      --include-build-deps \
      --no-check-signature \
      --fail-fast \
      --only dependencies; \
    fi && \
    if echo "$ENABLED_ENVS" | grep -q mpich; then \
    spack env create adios2-ci-mpich && \
    spack -e adios2-ci-mpich add ${ADIOS_SPEC_MPICH} && \
    spack -e adios2-ci-mpich concretize && \
    spack -e adios2-ci-mpich install \
      --include-build-deps \
      --no-check-signature \
      --fail-fast \
      --only dependencies; \
    fi && \
    spack clean -a && \
    echo "source /spack/share/spack/setup-env.sh" >> /etc/profile.d/zz-spack.sh

### Other missing packages (compared to el8 base):
RUN apt-get update && apt-get install -y \
    ccache \
    libcurl4-gnutls-dev && \
    apt-get autoremove --purge -y && \
    apt-get clean && \
    update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100 && \
    update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100 && \
    update-alternatives --install /usr/bin/gfortran gfortran /usr/bin/gfortran-11 100

ENV PATH=/opt/cmake/bin:${PATH}

ENTRYPOINT ["/bin/bash", "--login"]
